<template>
  <div class="app-container">
    <div id="map"></div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="员工姓名" prop="name">
        <el-select v-model="queryParams.name" clearable filterable placeholder="请选择" @change="handleQuery">
          <el-option v-for="(item, i) in options" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="电话" prop="phoneNumber">
        <el-input v-model="queryParams.phoneNumber" placeholder="请输入电话" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="适用区域" prop="belongAreaName">
        <el-select clearable v-model="queryParams.belongAreaName" @change="changeRegion">
          <el-option v-for="(item, index) of region" :key="index" :label="item.value" :value="item.value">{{
            item.value
          }}</el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="适用门店" prop="belongStoreName">
        <el-select clearable v-model="queryParams.belongStoreName" @change="handleQuery">
          <el-option v-for="(item, index) of store" :key="index" :label="item.value" :value="item.value">{{
            item.value
          }}</el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
          v-hasPermi="['business:technicianClock:query']">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>  -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        v-hasPermi="['business:technicianClock:edit']"
        >修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="storeList" max-height="650px" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="排序" align="center" prop="sort" /> -->
      <el-table-column label="排序" align="center" prop="sort" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.sort === null ? '无' : scope.row.sort }}</span>
        </template>
      </el-table-column>
      <el-table-column label="头像" align="center" prop="avatar" width="120">
        <template slot-scope="scope">
          <image-preview :src="scope.row.avatar" :width="50" :height="50" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="适用区域" align="center" prop="belongAreaName" width="120" />
      <el-table-column label="适用门店" align="center" prop="belongStoreName" /> -->
      <el-table-column label="员工姓名" align="center" prop="name" />
      <el-table-column label="工号" align="center" prop="jobNumber" />
      <el-table-column label="手机号" align="center" prop="phoneNumber" />
      <el-table-column label="轮数" align="center" prop="serviceOrderItemCounts" />
      <el-table-column label="综合评分" align="center" prop="score" />
      <!-- <el-table-column label="底薪" align="center" prop="basicSalary" />
      <el-table-column label="简介" align="center" prop="synopsis" :show-overflow-tooltip="true" />
      <el-table-column label="服务项目" align="center" prop="itemStoreList">
        <template slot-scope="scope">
          <template v-if="scope.row.itemStoreList && scope.row.itemStoreList.length > 0">
            <span>{{ scope.row.itemStoreList[0].itemName }}</span>
          </template>
          <template v-else>
            <span>暂无</span>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="在职" align="center" prop="onTheJob">
        <template slot-scope="scope">
          <span>{{ scope.row.onTheJob == '1' ? '在职' : '离职 ' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上架" align="center" prop="state">
        <template slot-scope="scope">
          <span>{{ scope.row.state == '1' ? '上架' : '下架 ' }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="入职时间" align="center" prop="entryTime" /> -->
      <!-- <el-table-column label="创建时间" align="center" prop="createTime" /> -->
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right"  width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:technicianClock:edit']"
          >修改</el-button>
          
          <el-button size="mini" type="text" icon="el-icon-download" @click="handleGround1(scope.row,onTheJob=0)"
             :disabled="scope.row.onTheJob==='1'">离职</el-button> 
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 添加或修改门店对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal='false'>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="员工排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入员工排序" style="width: 222px;" />
        </el-form-item>
        <!-- <el-form-item label="头像" prop="avatar">
          <image-upload v-model="form.avatar" />
        </el-form-item>
        <el-form-item label="区域" prop="belongAreaId">
            <el-select clearable v-model="form.belongAreaId" @change='changeRegion'>
            <el-option v-for="(item,index) of region" :key="index" :label="item.value" :value="item.key">{{item.value}}</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="适用门店" prop="belongStoreId" >
            <el-select clearable v-model="form.belongStoreId" @change='changeStore'>
            <el-option v-for="(item,index) of store" :key="index" :label="item.value" :value="item.key">{{item.value}}</el-option>
          </el-select>
        </el-form-item>
          <el-form-item label="员工姓名" prop="name">
            <el-input v-model="form.name" placeholder="请输入员工姓名" />
          </el-form-item>
          <el-form-item label="电话" prop="phoneNumber">
            <el-input v-model="form.phoneNumber" placeholder="请输入电话" />
          </el-form-item>
          <el-form-item label="工号" prop="jobNumber">
            <el-input v-model="form.jobNumber" placeholder="请输入工号" />
          </el-form-item>
          <el-form-item label="底薪" prop="basicSalary">
            <el-input v-model="form.basicSalary" placeholder="请输入底薪" />
          </el-form-item>
          <el-form-item label="在职" prop="onTheJob">
            <el-select clearable v-model="form.onTheJob" >
            <el-option v-for="(item,index) of List" :key="index" :label="item.value" :value="item.key">{{item.value}}</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="简介" prop="synopsis">
            <el-input v-model="form.synopsis" placeholder="请输入简介" />
          </el-form-item>
          <el-form-item label="上架" prop="state">
            <el-select clearable v-model="form.state" >
            <el-option v-for="(item,index) of List2" :key="index" :label="item.value" :value="item.key">{{item.value}}</el-option>
          </el-select>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listtechnician,
  gettechnician,
  delitetechnician,
  getClocklist,
  addtechnician,
  updatetechnician,
  listMemberRegion,
  listMemberStore,
} from "@/api/system/personnelMatters/technicianClock";

export default {
  name: "Store",
  data() {
    return {
      map: null,
      local: null,
      mk: null,
      latitude: "",
      longitude: "",
      keyWords: "",
      result: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 门店表格数据
      storeList: [],
      //员工姓名
      options: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        productName: null,
        storeName: null,
        storeName: null,
        phoneNumber: null,
        province: null,
        provinceString: null,
        city: null,
        cityString: null,
        county: null,
        countyString: null,
        detail: null,
        longitude: null,
        latitude: null,
        intro: null,
        images: null,
        state: "1",
        isInitiate: null,
        openingTime: null,
        closingTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      List: [
        {
          key: "1",
          value: "在职",
        },
        {
          key: "0",
          value: "离职",
        },
      ],
      List2: [
        {
          key: "0",
          value: "下架",
        },
        {
          key: "1",
          value: "上架",
        },
      ],
      region: [],
      store: [],
    };
  },
  created() {
    this.getList();
    this.listregion();
    this.getOptionsList()
    listMemberStore().then((res) => {
      console.log(res, 333);
      this.store = res.data.map((rItem) => {
        return {
          key: rItem.id,
          value: rItem.storeName,
        };
      });
    });
  },
  methods: {
    listregion() {
      listMemberRegion().then((res) => {
        this.region = res.data.map((rItem) => {
          return {
            key: rItem.id,
            value: rItem.regionName,
          };
        });
      });
    },
    changeRegion(value) {
      if (value) {
        this.handleQuery()
        const selectedOption = this.region.find((item) => item.value == value);
        if (!selectedOption) return
        const selectedKey = selectedOption.key;
        console.log(selectedKey, 'selectedKey');

        listMemberStore({ regionId: selectedKey }).then((res) => {
          console.log(res, 'res');

          this.store = res.data.map((rItem) => {
            return {
              key: rItem.id,
              value: rItem.storeName,
            };
          });
        });
      }
      
    },
    changeStore(value) {
      const selectedOption = this.store.find((item) => item.value == value);
      const selectedKey = selectedOption.key;
      const selectedValue = selectedOption.value;
      // this.form.belongStoreName = selectedValue;
      this.handleQuery()
    },
    /** 查询门店列表 */
    getList() {
      this.loading = true;
     // listtechnician(this.queryParams).then((response) => {
        getClocklist(this.queryParams).then((response) => {
        this.storeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //员工姓名
    getOptionsList() {
      const queryParams = {
        pageNum: 1,
        pageSize: 999,
        state: "1",
      }
      this.options = []
      listtechnician(queryParams).then((response) => {
        const map = new Map();
        const newArr = response.rows.filter(v => !map.has(v.jobNumber) && map.set(v.jobNumber, v));
        newArr.map(el => {
          this.options.push({
            label: el.name,
            value: el.name
          })
        })
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        storeName: null,
        phoneNumber: null,
        province: null,
        provinceString: null,
        city: null,
        cityString: null,
        county: null,
        countyString: null,
        detail: null,
        longitude: null,
        latitude: null,
        intro: null,
        images: null,
        status: null,
        isInitiate: null,
        openingTime: null,
        closingTime: null,
        createTime: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加技师排钟";
    },
    // 上下架
    handleGround1(row, onTheJob) {
      const params = {
        id: row.id,
        onTheJob: onTheJob,
      };
      this.$confirm(
        `此操作将${onTheJob === 1 ? "离职" : "下架"}该数据, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          updatetechnician(params).then((res) => {
            this.$message({
              type: "success",
              message: `${onTheJob === 1 ? "上架" : "下架"}成功!`,
            });
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消上下架",
          });
        });
    },
    handleGround(row, state) {
      const params = {
        id: row.id,
        state: state,
      };
      this.$confirm(
        `此操作将${state === 1 ? "上架" : "下架"}该工艺, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          updatetechnician(params).then((res) => {
            this.$message({
              type: "success",
              message: `${state === 1 ? "上架" : "下架"}成功!`,
            });
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      gettechnician(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改技师排钟";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updatetechnician(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.phoneNumber = parseInt(this.form.phoneNumber);
            addtechnician(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handReset(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认重置编号为"' + ids + '"的数据项？')
        .then(function () {
          return listResetPwd({ userId: ids });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("重置成功");
        })
        .catch(() => { });
    },
    // /** 删除按钮操作 */
    // handleDelete(row) {
    //   const ids = row.id || this.ids;
    //   this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function() {
    //     return delitetechnician(ids);
    //   }).then(() => {
    //     this.getList();
    //     this.$modal.msgSuccess("删除成功");
    //   }).catch(() => {});
    // },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/store/export",
        {
          ...this.queryParams,
        },
        `store_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
